﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EpiNEXT.Core;

namespace EpiNEXT.WPF
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:EpiNEXT.WPF"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:EpiNEXT.WPF;assembly=EpiNEXT.WPF"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:FixedCellTextBox/>
    ///
    /// </summary>
    public class FixedCellTextBox : ItemsControl
    {
        static FixedCellTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FixedCellTextBox), new FrameworkPropertyMetadata(typeof(FixedCellTextBox)));
        }

        public FixedCellTextBox()
            : base()
        {
            
        }

        protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);

            //if (this.ItemsSource == null)
            //{
            //    ObservableCollection<CharacterContainer> containerCollection = new ObservableCollection<CharacterContainer>();

            //    for (int i = 0; i < Cells; i++)
            //    {
            //        containerCollection.Add(new CharacterContainer());
            //    }

            //    this.ItemsSource = containerCollection;
            //}
            //else if(this.ItemsSource.Cast<CharacterContainer>().Count() != Cells)
            //{
            //    int length = this.ItemsSource.Cast<CharacterContainer>().Count();
            //    ObservableCollection<CharacterContainer> containerCollection = new ObservableCollection<CharacterContainer>();

            //    foreach (var item in this.ItemsSource)
            //    {
            //        containerCollection.Add(item as CharacterContainer);
            //    }

            //    for (int i = length - 1; i < this.Cells; i++)
            //    {
            //        containerCollection.Add(new CharacterContainer());
            //    }

            //    this.ItemsSource = containerCollection;
            //}
        }

        public static readonly DependencyProperty CellsProperty = DependencyProperty.Register("Cells", typeof(int), typeof(FixedCellTextBox), new PropertyMetadata(4));
        public int Cells
        {
            get
            {
                return (int)(this.GetValue(CellsProperty));
            }
            set
            {
                this.SetValue(CellsProperty, value);
            }
        }

        //public static readonly DependencyProperty FixedTextProperty =
        //    DependencyProperty.Register("FixedText", typeof(ObservableCollection<char>), typeof(FixedCellTextBox), null);

        //public ObservableCollection<char> FixedText
        //{
        //    get { return (ObservableCollection<char>)GetValue(FixedTextProperty); }
        //    set { SetValue(FixedTextProperty, value); }
        //}

        //public FixedCellTextBox()
        //    : base()
        //{
        //    DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, typeof(TextBox));
        //    //this.GetTemplateChild("")
        //    //dpd.AddValueChanged(this, 
        //}

        //private static void FixedTextPropertyChanged(DependencyObject dependencyObject,
        //                                   DependencyPropertyChangedEventArgs ea)
        //{
        //    FixedCellTextBox instance = dependencyObject as FixedCellTextBox;

        //    if (instance == null)
        //    {
        //        return;
        //    }

            
        //    //instance.Second = String.Format("First is {0}.", ((bool)ea.NewValue).ToString());

        //} // End method FirstPropertyChanged


        

        
        
    }
}
